Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Web
Imports System.Net.Mail

Public Class Tools

    Public Shared Sub Log(ByVal Message As String)

        log(Message, Nothing)

    End Sub

    Public Shared Sub Log(ByVal Message As String, ByVal Ex As Exception)

        Dim fileName As String = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "WroxUnited.log")

        Using logFile As New StreamWriter(fileName, True)

            logFile.WriteLine("{0}: {1}", DateTime.Now, Message)
            If Ex IsNot Nothing Then
                logFile.WriteLine(Ex.ToString())
            End If

            logFile.Close()

        End Using

    End Sub

    Public Shared Sub SendMail(ByVal Message As String)

        SendMail(Message, Nothing)

    End Sub

    Public Shared Sub SendMail(ByVal Message As String, ByVal Ex As Exception)

        Using msg As New MailMessage("website@wroxunited.net", "admin@wroxunited.net")

            msg.Subject = "WroxUnited.net Web Site Error"

            If Ex Is Nothing Then
                msg.Body = "There was an error on the website"""
            Else
                msg.Body = Ex.ToString()
            End If

            Dim client As New SmtpClient("MyMailServer")
            client.UseDefaultCredentials = True
            client.Send(msg)
        End Using

    End Sub

End Class
